package com.liyuan.help_e.dao;

import com.liyuan.help_e.model.Question;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface QuestionDao {
    @Select("select * from question order by modifiedAt desc limit #{size} offset #{offset} ")
    Question[] selectQuestionsByPage(@Param("offset") Integer offset,
                                     @Param("size") Integer size);

    @Select("select * from question order by readCount desc limit #{size}")
    Question[] selectQuestionsByReadCount(@Param("size") Integer size);

    @Select("select count(*) from question")
    Integer sumAllQuestions();

    @Select("select count(*) from question where tags like #{type}")
    Integer sumQuestionsByTag(String type);

    @Select("select * from question where tags like #{type} order by modifiedAt desc limit #{size} offset #{offset}")
    Question[] selectQuestionsByPageAndTag(@Param("offset") int i,
                                           @Param("size") Integer size,
                                           @Param("type") String type);


    @Select("select * from question where tags like #{type} order by modifiedAt desc limit #{size}")
    Question[] selectQuestionsByReadCountAndTag(@Param("size") Integer size, @Param("type") String s);

    @Select("select * from question where id = #{id}")
    Question selectQuestionById(Integer id);

    @Update("update question set commentCount = commentCount+1 where id = #{questionId}")
    void increCommentCount(Integer questionId);

    @Insert("insert into question(title,content,tags,createdAt,modifiedAt,user,commentCount,readCount,praiseCount) values(" +
            "#{title},#{content},#{tags},#{createdAt},#{modifiedAt},#{user.id},#{commentCount},#{readCount},#{praiseCount})")
    public void insertQuestion(Question question);

    @Update("update question set title = #{title},content=#{content},tags = #{tags},modifiedAt = #{modifiedAt},createdAt=#{createdAt},user = #{user.id},commentCount = #{commentCount},readCount = #{readCount},praiseCount = #{praiseCount} where id = #{id}")
    void updateQuestion(Question question_);


    @Select("select * from question where user = #{userId} order by modifiedAt desc limit #{size} offset #{offset}")
    Question[] selectQuestionsByUserId(@Param("offset") Integer offset,
                                       @Param("size") Integer size,
                                       @Param("userId") Integer userId);

    @Select("select count(*) from question where user = #{userId}")
    Integer sumQuestionsByUserId(Integer userId);
}
